//
//  MyKaQuanViewController.m
//  FD
//
//  Created by Yasin on 2018/10/18.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#define kRedLindWidth 45
#import "MyKaQuanViewController.h"
//view-cell
//controller
//#import "MyAlreayGetKaQuanViewController.h" //已经领取
#import "MyKaQuanTakenViewController.h"
#import "MyActivityRecommendKaQuanViewController.h" //活动推荐
@interface MyKaQuanViewController ()<UIScrollViewDelegate>
/**标题view*/
@property (nonatomic,  strong) UIView *titleView;
/**红色线*/
@property (nonatomic,  strong) UIView *redLineView;
/**当前选中的按钮*/
@property (nonatomic,  strong) UIButton *selectedBtn;
/**scrollView*/
@property (nonatomic,  strong) UIScrollView *scrollView;
@end

@implementation MyKaQuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //国际化
    self.navigationItem.title = NSLocalizedString(My_kaQuan, @"我的卡券");

    //添加子控制器
    [self setupChildViewControllers];
    
    [self setupScrollView];
    
    [self setupChildViewControllerViews]; //添加子控制器的view
    
    //设置标题
    [self setuTitleView];
    
//    if (self.fromWhichVC == 1) {
//        FDLog(@"从我的-->我的卡券进来的.导航条下一64px问题.");
//    }
}


//设置标题ivew
- (void)setuTitleView
{
    
    NSString *ylqStr = NSLocalizedString(L_YiLingQu, @"已领取");
    NSString *hdtjStr = NSLocalizedString(ActivityRecommend, @"活动推荐");
    
    NSArray *titleArr = @[ylqStr,hdtjStr];
    CGFloat x = 0;//20;
    CGFloat h = 40;
    CGFloat w = (SCREEN_WIDTH - x*2) / titleArr.count;
    
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, FD_NavH, SCREEN_WIDTH, h)];
    self.titleView.backgroundColor = [UIColor whiteColor];
//    self.titleView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.titleView];//添加
    
    for (int i=0; i<titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.tag = i;
        [btn setTitle:titleArr[i] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [btn addTarget:self action:@selector(clickTitleBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        //字体14号。
        [btn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
        
        //frame
        CGFloat btn_x = x + (w+x)*i;
        btn.frame  =CGRectMake(btn_x, 0, w, 38);
        [self.titleView addSubview:btn];
    }
    
    //默认第一个为选中按钮
    UIButton *firstBtn = self.titleView.subviews.firstObject;
    
    //底部指示器
    self.redLineView = [UIView new];
    self.redLineView.backgroundColor = [UIColor redColor];
    self.redLineView.rb_height = 2;
    self.redLineView.rb_y = self.titleView.rb_height - 2;
    self.redLineView.rb_width = kRedLindWidth; //线的宽度固定40
    [self.titleView addSubview:self.redLineView];
    
    //设置默认选中按钮
    [self titleClick:firstBtn];
}
- (void)titleClick:(UIButton *)btn
{
    self.selectedBtn.selected  = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
    
    //指示器移动位置
    [UIView animateWithDuration:0.25 animations:^{
        self.redLineView.rb_center_x = btn.rb_center_x;
    }];
    
    //!!!!:待实现？让scrollView 滚到对应的位置
    
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = btn.tag * _scrollView.rb_width;
    [self.scrollView setContentOffset:offset animated:YES];
}

- (void)clickTitleBtnAction:(UIButton *)btn
{
    NSLog(@"点击了:%ld---%@",btn.tag,btn.titleLabel.text);
    [self titleClick:btn];
}

//设置子控制器
- (void)setupChildViewControllers
{
    MyKaQuanTakenViewController *alreadyVC = [[MyKaQuanTakenViewController alloc] init];
    [self addChildViewController:alreadyVC];
    
    MyActivityRecommendKaQuanViewController *activityVC = [[MyActivityRecommendKaQuanViewController alloc] init];
    [self addChildViewController:activityVC];
}

//设置scrollView
- (void)setupScrollView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.frame = self.view.bounds;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    //子控制器个数
    NSUInteger count = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(count * scrollView.rb_width, 0);
    
    self.scrollView = scrollView; //赋值
}
//添加子控制器view
- (void)setupChildViewControllerViews {
    
    //索引
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.rb_width;
    
    //取出子视图控制
    UIViewController *childVC = self.childViewControllers[index];
    
    //优化代码
    if ([childVC isViewLoaded]) return;
    
    //添加子控制器的view
    childVC.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:childVC.view];
}


#pragma mark - 一 UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self setupChildViewControllerViews];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger index = scrollView.contentOffset.x / scrollView.rb_width;
    
    //更新顶部标题选中状态
    UIButton *selectedBtn = self.titleView.subviews[index];
    [self titleClick:selectedBtn];
    
    //子控制器添加到scrollView上
    [self setupChildViewControllerViews];
}

#pragma mark - 二 请求数据
///**请求优惠券详情列表数据*/
//- (void)loadCouponListData {
//    [FDHttpAPi requestSYCouponListSuccessBlock:^(id responseObjc) {
//        
//    } failBlock:^(NSError *error) {
//        
//    }];
//}


@end
